<?php
/*
//  Copyright (C) 2007 Max Plischke
//  Original Author: Max Plischke <plischke@gmail.com>
//  Link: http://code.google.com/p/open-club-directory/
// 
//  This file is part of the Open-Club-Directory Project.
//
//  Open-Club-Directory is free software: you can redistribute it and/or 
//  modify it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  Open-Club-Directory is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Open-Club-Directory.
//  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'appinclude.php';
require_once 'funcinclude.php';

$link = mysql_connect($_mysql_server, $_mysql_user, $_mysql_password) or die();
mysql_select_db($_mysql_db) or die();

if ($is_app_added==true) { 
	$member = member($user);
} else {
	mysql_close($link);
	echo '<fb:redirect url="'.$appcallbackurl.'">';
	exit;
}

if ($member!=false) {
	// Get list of friends who have this app installed... 
	$rs = $facebook->api_client->fql_query("SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = $user)"); $arFriends = ""; 
	
	// Build an delimited list of users... 
	if ($rs) { for ( $i = 0; $i < count($rs); $i++ ) { if ( $arFriends != "" ) $arFriends .= ","; $arFriends .= $rs[$i]["uid"]; } }
 
	$content = "<fb:req-choice url=\"".$appcallbackurl."?confirm_invite=1\" label=\"Confirm & Add Application\" /><fb:name uid=\"$user\"/> says you are a member of ".$_ClubName.". Would you like to confirm your membership and add the ".$_ClubName." Application?";

	echo '<fb:explanation message="You have '.count($rs).' '.$_ClubName.' Friends">'.count($rs).' of your friends are already members of the '.$_ClubName.' Application! Invite more friends below!</fb:explanation>';
	echo '<fb:request-form invite="true" type="'.$_ClubName.'" content="'.htmlentities($content).'" invite="true" action="'.$appcallbackurl.'">';
	echo '<fb:multi-friend-selector actiontext="Select the friends you want to invite to the '.$_ClubName.' Application" rows="3" exclude_ids="'.$arFriends.'"/>';
	echo '</fb:request-form>';
	echo '<fb:google-analytics uacct="'.$_google_analytics_account.'" />';

	mysql_close($link);
} else {
	mysql_close($link);
	echo '<fb:redirect url="'.$appcallbackurl.'">';
}

?>